package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.DishType;
import com.pojo.Msg;
import com.service.DishTypeService;
import com.shiro.controller.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@RequiresPermissions("foodTypeTable:*")
@Controller
public class DishTypeController extends BaseController {
    @Autowired
    private DishTypeService dishTypeService;

    @RequestMapping("/dishtype")
    @ResponseBody
    public Msg getDishType() {
        List<DishType> list = dishTypeService.getDishType();
        return Msg.success().add("dishtype",list);
    }

    //增加食品类型
    @RequestMapping(value="/dishtype", method = RequestMethod.POST)
    @ResponseBody
    public Msg saveDishType(@RequestBody DishType dishType, BindingResult bindingResult){
//        if(dishTypeService.checkDishTypebyName(dishType.getDtName())){
//
//        }
        if(bindingResult.hasErrors()){
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = bindingResult.getFieldErrors();
            for(FieldError fieldError: errors){
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return Msg.fail().add("errorFields", map);
        }else{
            //重名了
            if(!dishTypeService.checkDishTypebyName(dishType.getDtName())){
                return Msg.fail().add("errorMessage", "菜品类型名重复");
            }else{
                dishTypeService.saveDishType(dishType);
                return Msg.success();
            }
        }

    }

    //删除食品类别, {dtId} 绑定了 参数栏中的 dtId
    @RequestMapping(value="/dishtype/{dtId}", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteDishType(@PathVariable("dtId") String dtId){
        Integer id = Integer.parseInt(dtId);
        dishTypeService.delectDishType(id);
        return Msg.success();
    }

    //修改食品类别
    @RequestMapping(value = "/dishtype/{dtId}", method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateDishType(@RequestBody DishType dishType){
        dishTypeService.updateDishType(dishType);
        return Msg.success();
    }


    //模糊查询 采用分页查询 使用 pageHelper插件,当需要显示全部时，可以空串
    @RequestMapping(value="/dishtype/find", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Msg getDishTypeByName(@RequestParam(value="dtName", defaultValue = "") String dtName,  @RequestParam(value = "pn", defaultValue = "1") Integer pn){

        System.out.println(pn+"页码");
        // 在查询之前只需要调用，传入页码，以及每页的大小10，默认一页
        PageHelper.startPage(pn, 10);

        List<DishType> dishTypeList = dishTypeService.getDishTypeByName(dtName);

        PageInfo page = new PageInfo(dishTypeList, 10);

        Msg msg = Msg.success().add("pageInfo", page);

        return msg;
    }
}
